package com.ilop.siteraudio.data.util;

import com.ilop.siteraudio.data.bean.RecordBean;
import com.ilop.siteraudio.data.manage.CommonDaoUtils;
import com.ilop.siteraudio.data.manage.DaoManager;
import com.ilop.siteraudio.greendao.RecordBeanDao;

/**
 * @author skygge
 * @date 2020/12/7.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class RecordDaoUtil {

    private volatile static RecordDaoUtil instance = null;
    private final CommonDaoUtils<RecordBean> mRecordUtils;
    private final RecordBeanDao mRecordDao;

    public static RecordDaoUtil getInstance(){
        if (instance == null){
            synchronized (RecordDaoUtil.class){
                if (instance == null){
                    instance = new RecordDaoUtil();
                }
            }
        }
        return instance;
    }

    private RecordDaoUtil() {
        DaoManager mManager = DaoManager.getInstance();
        mRecordDao = mManager.getDaoSession().getRecordBeanDao();
        mRecordUtils = new CommonDaoUtils(RecordBean.class, mRecordDao);
    }

    public CommonDaoUtils<RecordBean> getRecordDao(){
        return mRecordUtils;
    }

    public RecordBean getRecordById(long recordId) {
        return mRecordDao.queryBuilder()
                .where(RecordBeanDao.Properties.RecordId.eq(recordId))
                .unique();
    }

    public long getMaxRecordId() {
        RecordBean recordBean = mRecordDao.queryBuilder()
                .orderDesc(RecordBeanDao.Properties.Id)
                .offset(0)
                .limit(1)
                .unique();
        if (recordBean == null) {
            return 0;
        } else {
            return recordBean.getId();
        }
    }
}
